$(function () {
    layui.config({
        base: './../../../static/lib/dist/'
    });
    layui.use([ 'laydate', 'form', 'notice', 'table'],function () {
        var form = layui.form;
        var notice = layui.notice;
        var table = layui.table;
        var laydate = layui.laydate;

        function inittable() {
            let initjson = {
                elem: '#agencylist',
                url: 'http://127.0.0.1:8098/amc/sys/base/agency/page',
                params: {
                    agencyName: $('#agencyName').val()
                },
                headers: {'authorization': layui.data('user').token},
                id: 'agency_tab',
                cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'agencyCode', title: '机构编号', sort: true}
                    ,{field:'agencyName', title: '机构名称'}
                    ,{field:'agencyAddr', title: '机构地址'}
                    ,{field:'remark', title: '备注'}
                    ,{field:'parCode', title: '上级机构', templet: function (res) {
                            if(res.parCode == null){
                                return ''
                            }else{
                                return res.parAgencyInfo.agencyName
                            }
                        }}
                    ,{field:'createTime', title: '创建时间'}
                    ,{field:'createUser', title: '创建人', templet:function (res) {
                            if(res.createUser !=null){
                                return res.createUserInfo.name
                            }else{
                                return ''
                            }
                        }}
                    ,{field:'lastUpdateTime', title: '最近更新时间'}
                    ,{field:'lastUpdateUser', title: '最近更新人', templet:function (res) {
                            if(res.lastUpdateUser !=null){
                                return res.lastUpdateUserInfo.name
                            }else{
                                return ''
                            }
                        }}
                    ,{field:'cancel', title: '状态', templet: function (res) {
                            if(res.cancel){
                                return '<span style="color: #F581B1;">注销</span>'
                            }else{
                                return ''
                            }
                        }}
                    ,{fixed: 'right', title:'操作', toolbar: '#agencybar', width:150}
                ]]
            }
            tableinit(table, initjson)
        }
        inittable()

        form.on('submit(search)', function (data) {
            table.reload('agency_tab', {
                where: {
                    agencyName: data.field.agencyName
                } //设定异步数据接口的额外参数
            });
        })

        $('#delAll').click(function () {
            let checkStatus = table.checkStatus('agency_tab');
            let checkdata = checkStatus.data
            if(checkdata.length <= 0){
                notice.warning('请选择要删除的机构')
                return false
            }
            let agencyCodes = []
            checkdata.forEach(cd => {
                agencyCodes.push(cd.agencyCode)
            })
            layer.confirm('确认要删除吗？',function(index){
                deletes(agencyCodes).then(value => {
                    if(value.code == 200){
                        layer.msg('删除成功', {icon: 1});
                        tableReload()
                    }else{
                        layer.msg('删除失败，'+value.msg)
                    }
                }).catch(reason => {
                    layer.msg('服务器未连结')
                })

            });
        })

        function tableReload(){
            table.reload('agency_tab', {
                where: {
                    agencyName: $('#agencyName').val()
                } //设定异步数据接口的额外参数
            });
        }

        //监听行工具事件
        table.on('tool(agencyfilter)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确认要删除吗', function(index){
                    let agencyCodes = []
                    agencyCodes.push(data.agencyCode)
                    deletes(agencyCodes).then(value => {
                        if(value.code == 200){
                            layer.msg('删除成功', {icon: 1});
                            tableReload()
                        }else{
                            layer.msg('删除失败，'+value.msg)
                        }
                    }).catch(reason => {
                        layer.msg('服务器未连结')
                    })
                });
            } else if(obj.event === 'cancel'){
                layer.confirm('确认要更改状态吗', function(index){
                    let agencyCodes = []
                    agencyCodes.push(data.agencyCode)
                    cancel(agencyCodes).then(value => {
                        if(value.code == 200){
                            layer.msg('更改成功', {icon: 1});
                            tableReload()
                        }else{
                            layer.msg('更改失败，'+value.msg)
                        }
                    }).catch(reason => {
                        layer.msg('服务器未连结')
                    })
                });
            } else if(obj.event === 'edit'){
                xadmin.open('机构更新','./agency-edit.html',800,600)
                layui.data('agency', {
                    key: 'edit',
                    value: data
                })
            }
        });
    });


})